﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using DBUtility;
using MySql.Data.MySqlClient;

namespace DAL
{
    public class DTrade
    {
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public  bool Add(Model.TradeInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into ttrade(");
            strSql.Append("Type,phoneNumber,tradetime,phoneNumber2)");
            strSql.Append(" values (");
            strSql.Append("@Type,@phoneNumber,@tradetime,@phoneNumber2)");
            MySqlParameter[] parameters = {
					new MySqlParameter("@Type", MySqlDbType.Int32,10),
					new MySqlParameter("@phoneNumber", MySqlDbType.VarChar,15),
					new MySqlParameter("@tradetime", MySqlDbType.DateTime),
					new MySqlParameter("@phoneNumber2", MySqlDbType.VarChar,15)};
            parameters[0].Value = model.Type;
            parameters[1].Value = model.PhoneNumber;
            parameters[2].Value = model.TradeTime;
            parameters[3].Value = model.PhoneNumber2;

            try
            {
                int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
                if (rows > 0)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                Common.CommonRoutine.HandlingExceptions(ex);
                return false;
            }

        }


        #region  Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public  int GetMaxId()
        {
            return DbHelperMySQL.GetMaxID("id", "ttrade");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public  bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from ttrade");
            strSql.Append(" where id=@id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int32)
};
            parameters[0].Value = id;

            return DbHelperMySQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 更新一条数据
        /// </summary>
        public  bool Update(Model.TradeInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update ttrade set ");
            strSql.Append("Type=@Type,");
            strSql.Append("phoneNumber=@phoneNumber,");
            strSql.Append("TradeTime=@TradeTime,");
            strSql.Append("phoneNumber2=@phoneNumber2");
            strSql.Append(" where id=@id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@Type", MySqlDbType.Int32,10),
					new MySqlParameter("@phoneNumber", MySqlDbType.VarChar,15),
					new MySqlParameter("@TradeTime", MySqlDbType.DateTime),
					new MySqlParameter("@phoneNumber2", MySqlDbType.VarChar,15),
					new MySqlParameter("@id", MySqlDbType.Int32,10)};
            parameters[0].Value = model.Type;
            parameters[1].Value = model.PhoneNumber;
            parameters[2].Value = model.TradeTime;
            parameters[3].Value = model.PhoneNumber2;
            parameters[4].Value = model.Id;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public  bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from ttrade ");
            strSql.Append(" where id=@id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int32)
};
            parameters[0].Value = id;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public  bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from ttrade ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public  Model.TradeInfo GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,Type,phoneNumber,time,phoneNumber2 from ttrade ");
            strSql.Append(" where id=@id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int32)
};
            parameters[0].Value = id;

            Model.TradeInfo model = new Model.TradeInfo();
            DataSet ds = DbHelperMySQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"] != null && ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Type"] != null && ds.Tables[0].Rows[0]["Type"].ToString() != "")
                {
                    model.Type = int.Parse(ds.Tables[0].Rows[0]["Type"].ToString());
                }
                if (ds.Tables[0].Rows[0]["phoneNumber"] != null && ds.Tables[0].Rows[0]["phoneNumber"].ToString() != "")
                {
                    model.PhoneNumber = ds.Tables[0].Rows[0]["phoneNumber"].ToString();
                }
                if (ds.Tables[0].Rows[0]["TradeTime"] != null && ds.Tables[0].Rows[0]["TradeTime"].ToString() != "")
                {
                    model.TradeTime = DateTime.Parse(ds.Tables[0].Rows[0]["TradeTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["phoneNumber2"] != null && ds.Tables[0].Rows[0]["phoneNumber2"].ToString() != "")
                {
                    model.PhoneNumber2 = ds.Tables[0].Rows[0]["phoneNumber2"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public  DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,");//
            strSql.Append("(CASE Type WHEN 2 THEN '平台' WHEN 1 THEN '乘客' WHEN 0 THEN '出租车'   END) AS Type");
            strSql.Append(",phoneNumber,TradeTime,phoneNumber2 ");
            strSql.Append(" FROM ttrade ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by id desc ");
            return DbHelperMySQL.Query(strSql.ToString());
        }

        public DataSet GetReport(string strWhere)
        { 
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT count(*) as Total,");//         
            strSql.Append("count(case Type when '0' then Type end)as Taxi,");
            strSql.Append("count(case Type when '1' then Type end) as Passenger,");
            strSql.Append("count(case Type when '2' then Type end) as Platform,");
            strSql.Append("DATE_FORMAT(tradetime, '%m-%d' ) AS DT");
            strSql.Append(" From ttrade");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" group by DT order by DT asc"); 
            return DbHelperMySQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public  int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM ttrade ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public  DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.id desc");
            }
            strSql.Append(")AS Row, T.*  from ttrade T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
          
            return DbHelperMySQL.Query(strSql.ToString());
        }
        

        #endregion  Method
    }
}
